<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 搜索区域 -->
      <el-row :gutter='20'>
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="reQuery">
            <el-button slot="append" icon="el-icon-search" @click="queryGoods"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表 -->
      <el-table :data="goodsList" stripe border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="600px"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格" ></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column prop="goods_number" label="商品数量"></el-table-column>
        <el-table-column prop="add_time" label="商品创建时间" width="400px">
          <template slot-scope="scope">
            {{scope.row.add_time|dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="editGoods(scope.row)" >编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="deleteGoods(scope.row.goods_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区域  -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total" background>
      </el-pagination>
      <!-- 编辑区域对话框 -->
      <el-dialog
        title="编辑信息"
        :visible.sync="editDialogVisible"
        width="30%">
        <!-- 商品信息 -->
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
          <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="editForm.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="goods_price">
            <el-input v-model="editForm.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品重量" prop="goods_weight">
            <el-input v-model="editForm.goods_weight"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="goods_number">
            <el-input v-model="editForm.goods_number"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="commitEdit">确 定</el-button>
        </span>
      </el-dialog>

    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 请求参数获取商品列表
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 接收商品列表数据
      goodsList: [],
      // 总数据条数
      total: 0,
      // 编辑区域对话框
      editDialogVisible: false,
      // 编辑提交商品
      editForm: {},
      // 接收id
      editId: '',
      // 编辑对话框中的验证规则
      editFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 请求商品数据列表
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods',
        { params: this.queryInfo })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 每页展示多少数据改变时触发，传入展示的数据条数
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 当前页改变时触发
    handleCurrentChange (newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },
    // 点击查询商品
    queryGoods () {
      if (this.queryInfo.query === '') {
        this.$message.error('请输入搜索内容')
        return
      }
      this.getGoodsList()
    },
    // 清除搜索框再获取列表
    reQuery () {
      this.queryInfo.query = ''
      this.getGoodsList()
    },
    // 删除对应商品
    async deleteGoods (id) {
      const res = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(res)
      if (res !== 'confirm') {
        return this.$message('取消了删除')
      }
      // 发送请求
      const { data: param } = await this.$http.delete(`goods/${id}`)
      console.log(param)
      if (param.meta.status !== 200) {
        this.$message.error('删除商品失败')
      }
      this.getGoodsList()
      this.$message.success('删除商品成功')
    },
    // 编辑商品
    editGoods (scopeRow) {
      console.log(scopeRow)
      this.editForm = scopeRow
      this.editId = scopeRow.goods_id
      console.log(this.editForm)
      this.editDialogVisible = true
    },
    // 提交编辑
    commitEdit (id) {
      // 表单预校验
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请输入合法的表单信息')
        }
        // 发送请求
        const { data: res } = await this.$http.put(`goods/${this.editId}`, this.editForm)
        console.log(res)
        // if (res.meta.status !== 200) {
        //   return this.$message('此接口暂时有问题')
        // }
        this.editDialogVisible = false
      })
    },
    // 跳转到添加商品页面
    goAddPage () {
      this.$router.push('/goods/add')
    }

  }
}
</script>

<style lang="less" scoped>
</style>
